package io.renren.modules.wyzsphone.service.Impl;


import io.renren.modules.email.Email;
import io.renren.modules.wyzs.dao.PasswordModifyMapper;
import io.renren.modules.wyzsphone.service.PasswordModifyService;
import io.renren.modules.sys.dao.SysUserDao;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class PasswordModifyServiceImpl implements PasswordModifyService {
    @Autowired
    PasswordModifyMapper passwordModifyMapper;
    @Autowired
    SysUserDao sysUserDao;

    @Override
    public void PasswordModify(String stu_id,String old_password,String new_password){

       passwordModifyMapper.PasswordModify(stu_id,old_password,new_password);
    }

    @Override
    public SysUserEntity getSysUserByUsernameAndEmail(String username, String email) {
        return passwordModifyMapper.getSysUserByUsernameAndEmail(username,email);
    }

    @Override
    /*
    *随机产生8位密码 ，并发送邮件
     */
    public void retrievePassword(SysUserEntity user) throws Exception {

        String uuid = UUID.randomUUID().toString().substring(8);
        String uuidStr=uuid.replace("-", "");
        String newPassword=uuidStr.substring(0,7);
        user.setPassword(new Sha256Hash(newPassword, user.getSalt()).toHex());
        sysUserDao.update(user);
        String title="滨州职业学院教学记载与评价系统：密码修改通知";
        StringBuffer content=new StringBuffer();
        content.append(user.getFullName());
        content.append("同学：<br>&nbsp;&nbsp;&nbsp;&nbsp");
        content.append("您在滨州职业学院教学记载与评价系统中的密码修改为：");
        content.append(newPassword);
        content.append("<br><br><br><br><br>");
        content.append("<label style=\"text-align:right\">滨州职业学院教务处</label>");
        Email.setEmail(user,title,content.toString());

    }
}
